
function html_rpc_receive() 
{
	//document.getElementById("chat").innerHTML += "<p class=\"chat_line\"><a class=\"name\">Info:</a> start</p>";
	var xhr = getXmlHttpRequestObject();
	if(!xhr) {
		// TODO: No XMLHttpRequest Support - what to do?
		alert("Sorry, but the messages_sender library does not work with your browser.");
	}
	else
	{
		xhr.onreadystatechange = function()
		{
			if(xhr.readyState == 4)
			{
				if(xhr.status == 200)
				{
					// We have an event.
					var response = xhr.responseText;
					var validIndex = 0;
					var aux = response;
					var index = aux.lastIndexOf("-");
					while(index > 0)
					{
						validIndex = index;
						aux = aux.slice(0, index);
						index = aux.lastIndexOf("-");
					}
					
					if(validIndex > 0)
					{
						var action = response.slice(0, validIndex);
						
						// Message
						if(action == "1")
						{
							validIndex = 0;
							aux = response;
							index = aux.lastIndexOf(":");
							while(index > 0)
							{
								validIndex = index;
								aux = aux.slice(0, index);
								index = aux.lastIndexOf(":");
							}
					
							if(validIndex > 0)
							{
								var chat_div = document.getElementById("chat");
								var name = response.slice(2, validIndex);
								var message = response.slice(validIndex + 1, response.length);
								chat_div.innerHTML += 
									"<p class=\"chat_line\"><a class=\"name\">" + 
									name + 
									":</a> " +
									message +
									"</p>";
								chat_div.scrollTop = chat_div.scrollHeight;
							}
						}
						//Add to user list
						else if(action == "2")
						{
							var name = response.slice(2, response.length);
							add_to_user_list(name);
						}
						//Remove from user list
						else if(action == "3")
						{
							var name = response.slice(2, response.length);
							remove_from_user_list(name);
						}
						//Create the user list
						else if(action == "4")
						{
							var list = response.slice(2, response.length);
							create_user_list(list);
						}
					}
					
					html_rpc_receive();
				}
			}
		}
		xhr.open("POST", "/yaws/chat_receive_message.yaws", true);			
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send(null);
	}     
}


//Function for initializating the page.
function startChat()
{
	//Set the focus to the Message Box.
	document.getElementById('chat_message').focus();

	html_rpc_receive();
}		


//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject()
{
	var xhr = false;
	if(window.XMLHttpRequest)
	{
		xhr = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {}
		}
	}
	return xhr;
}


//Add a message to the chat server.
function sendChatText()
{
	if(document.getElementById("chat_message").value == "")
	{
		alert("You have not entered a message");
		return;
	}

	var sendReq = getXmlHttpRequestObject();			
	sendReq.open("POST", "/yaws/chat_send_message.yaws", true);
	sendReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	var param = "message=" + document.getElementById("chat_message").value;
	sendReq.send(param);
	document.getElementById("chat_message").value = "";	
}


//This functions handles when the user presses enter.  Instead of submitting the form, we
//send a new message to the server and return false.
function blockSubmit()
{
	sendChatText();
	return false;
}


//Adds the name of the user to the user list in the chat page
function add_to_user_list(user)
{
	var list = document.getElementById("userlist");
	var anchor = document.createElement("a");
	anchor.appendChild(document.createTextNode(user));
	list.appendChild(anchor);
}


//Removes the name of the user from the user list in the chat page
function remove_from_user_list(user)
{
	var list = document.getElementById("userlist");
	
	var i = 0;
	while(list.hasChildNodes() == true)
	{
		if(list.childNodes[i].innerHTML == user)
		{
			list.removeChild(list.childNodes[i]);
			return false;
		}
		i++;
	}
}


//Initialises the user list adding to it all the users in the parameter list
function create_user_list(to_add)
{
	var list = document.getElementById("userlist");
	while(list.hasChildNodes() == true)
	{
		list.removeChild(list.childNodes[0]);
	}
	
	var title = document.createElement("a");
	title.appendChild(document.createTextNode("Online users"));
	title.setAttribute("class", "titlelist");
	list.appendChild(title);
	
	var indexFirst = to_add.lastIndexOf(":");
	var indexLast = to_add.length;
	while(indexFirst != -1)
	{
		var name = to_add.slice(indexFirst + 1, indexLast);
		var anchor = document.createElement("a");
		anchor.appendChild(document.createTextNode(name));
		list.appendChild(anchor);
		
		var remain_to_add = to_add.slice(0, indexFirst);
		indexLast = indexFirst;
		indexFirst = remain_to_add.lastIndexOf(":");
	}
}
